define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rexam/rexam/index' + location.search,
                    add_url: 'rexam/rexam/add',
                    edit_url: 'rexam/rexam/edit',
                    del_url: 'rexam/rexam/del',
                    multi_url: 'rexam/rexam/multi',
                    import_url: 'rexam/rexam/import',
                    table: 'rexam',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'headfile', title: __('Headfile'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'posterfile', title: __('Posterfile'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'areadata', title: __('Areadata'), searchList: {"0":__('Areadata 0'),"1":__('Areadata 1'),"2":__('Areadata 2')}, formatter: Table.api.formatter.normal},
                        {field: 'question_count', title: __('Question_count')},
                        {field: 'limitdata', title: __('Limitdata'), searchList: {"0":__('Limitdata 0'),"1":__('Limitdata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'randomdata', title: __('Randomdata'), searchList: {"0":__('Randomdata 0'),"1":__('Randomdata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'pointsdata', title: __('Pointsdata'), searchList: {"0":__('Pointsdata 0'),"1":__('Pointsdata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'pointslimit', title: __('Pointslimit')},
                        {field: 'groupdata', title: __('Groupdata'), searchList: {"0":__('Groupdata 0'),"1":__('Groupdata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'anadata', title: __('Anadata'), searchList: {"0":__('Anadata 0'),"1":__('Anadata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'expiretime', title: __('Expiretime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            areaChange();
            limitChange();
            pointsChange();
            groupChange();
            bindChange();
            $("input:radio[name='row[areadata]']").click(function(){
                areaChange();
            })
            $("input:radio[name='row[limitdata]']").click(function(){
                limitChange();
            })
            $("input:radio[name='row[pointsdata]']").click(function(){
                pointsChange();
            })
            $("input:radio[name='row[groupdata]']").click(function(){
                groupChange();
            })
            $("input:radio[name='row[binddata]']").click(function(){
                bindChange();
            })
        },
        edit: function () {
            Controller.api.bindevent();
            areaChange();
            limitChange();
            pointsChange();
            groupChange();
            bindChange();
            $("input:radio[name='row[areadata]']").click(function(){
                areaChange();
            })
            $("input:radio[name='row[limitdata]']").click(function(){
                limitChange();
            })
            $("input:radio[name='row[pointsdata]']").click(function(){
                pointsChange();
            })
            $("input:radio[name='row[groupdata]']").click(function(){
                groupChange();
            })
            $("input:radio[name='row[binddata]']").click(function(){
                bindChange();
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    //考试范围
    function areaChange(){
        var area = $("input:radio[name='row[areadata]']:checked").val();
        switch (area){
            case '0':
                $("#gSubject").hide();
                $("#gQuestion").hide();
                break;
            case '1':
                $("#gSubject").show();
                $("#gQuestion").hide();
                break;
            case '2':
                $("#gSubject").hide();
                $("#gQuestion").show();
                break;
        }
    }

    //考试限时
    function limitChange(){
        var limit = $("input:radio[name='row[limitdata]']:checked").val();
        switch (limit){
            case '0':
                $("#gLimitOne").hide();
                $("#gLimitAll").hide();
                break;
            case '1':
                $("#gLimitOne").show();
                $("#gLimitAll").show();
                break;
        }
    }

    //打分规则
    function pointsChange(){
        var points = $("input:radio[name='row[pointsdata]']:checked").val();
        switch (points){
            case '0':
                $("#gPointslimit").hide();
                break;
            default:
                $("#gPointslimit").show();
                break;
        }
    }

    //指定考生组
    function groupChange(){
        var group = $("input:radio[name='row[groupdata]']:checked").val();
        console.log(group);
        switch (group){
            case '1':
                $("#gGroup").show();
                break;
            default:
                $("#gGroup").hide();
                break;
        }
    }

    //信息綁定
    function bindChange(){
        var bind = $("input:radio[name='row[binddata]']:checked").val();
        console.log(bind);
        switch (bind){
            case '1':
                $("#gBind").show();
                break;
            default:
                $("#gBind").hide();
                break;
        }
    }

    return Controller;
});